<template>
  <div>
    <el-card>
    <el-button type="success" @click="dialogaddVisible.visible = true"
      ><i-ep-plus />发布告示</el-button
    >

    <el-table
      :data="dataList"
      style="width: 100%"
      class="mt-5"
      v-loading="loading"
			:header-cell-style="{'text-align':'center'}"
    			:cell-style="{'text-align':'center'}"
    >
      <el-table-column
        fixed
        type="index"
        :index="indexMethod"
        label="序号"
        :width="80"
      />
      <el-table-column prop="messageType" label="消息类型" :width="108" />
      <el-table-column type="expand">
        <template #default="props">
          <v-md-preview :text="props.row.content" />
        </template>
      </el-table-column>
      <el-table-column
        prop="content"
        label="内容"
        :show-overflow-tooltip="true"
      />
      <el-table-column prop="createTime" label="发布时间" :width="220" />
      <el-table-column fixed="right" label="操作" :width="220">
        <template #default="scope">
          <el-button type="warning" round @click="openEditDialog(scope.row)"
            >编辑</el-button
          >
          <el-button type="danger" round @click="deleteClick(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="flex justify-center w-full mt-5">
      <el-pagination
        background
        layout="sizes, prev,slot, next, jumper"
        :page-count="pageTotal"
        :page-sizes="[10, 20, 30, 50]"
        :current-page="queryParams.pageNum"
        @update:current-page="pageChange"
        @update:page-size="sizeChange"
        :default-current-page="1"
        :default-page-size="10"
      >
        <div class="pagination">
          <span
            class="pagination__item"
            :class="{ active: queryParams.pageNum === pager }"
            v-for="pager in pageArr"
            :key="pager"
            @click="pageChange(pager)"
          >
            {{ pager }}
          </span>
        </div>
      </el-pagination>
    </div>
  </el-card>

  <el-dialog
    v-model="dialogEditVisible.visible"
    title="告示修改"
    width="70%"
    top="5vh"
  >
    <el-form :model="formData">
      <el-form-item label="内容" :label-width="50">
        <v-md-editor
          v-model="formData.content"
          height="480px"
          placeholder="请输入内容"
          :disabled-menus="[]"
          @upload-image="handleUploadImage"
        ></v-md-editor>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogEditVisible.visible = false">取消</el-button>
        <el-button type="primary" @click="editInfoClick(formData)">
          修改
        </el-button>
      </span>
    </template>
  </el-dialog>

  <el-dialog
    v-model="dialogaddVisible.visible"
    title="告示发布"
    width="70%"
    top="5vh"
  >
    <el-form :model="addData">
      <el-form-item label="内容" :label-width="50">
        <v-md-editor
          v-model="addData.content"
          height="480px"
          placeholder="请输入内容"
          :disabled-menus="[]"
          @upload-image="handleUploadImage"
        ></v-md-editor>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogaddVisible.visible = false">取消</el-button>
        <el-button type="primary" @click="addInfoClick"> 发布 </el-button>
      </span>
    </template>
  </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {
  getMessage,
  deleteNotice,
  publishNotice,
  updateNotice,
} from "@/api/systemNotice";
import { messageInfo, pageParams } from "@/api/systemNotice/type";
import axios from "axios";

import { onMounted, reactive, ref } from "vue";

const dialogEditVisible = reactive<DialogOption>({
  visible: false,
});

const dialogaddVisible = reactive<DialogOption>({
  visible: false,
});

const queryParams = reactive<pageParams>({
  pageNum: 1,
  pageSize: 10,
});
//总共页数
const pageTotal = ref(0);
//后端传来的页码数组
const pageArr = ref([]);

const dataList = ref<messageInfo[]>();
const loading = ref(false);
const formData = reactive<messageInfo>({
  id: 0,
  recipient: "",
  messageType: "",
  content: "",
  readStatus: "",
  createTime: "",
});

const addData = reactive<messageInfo>({
  content: "",
});

//编辑打开弹窗
async function openEditDialog(row: messageInfo) {
  dialogEditVisible.visible = true;
  dialogEditVisible.title = "修改数据";
  Object.assign(formData, row);
}

//新增打开弹窗
async function openDialog(row: messageInfo) {
  dialogEditVisible.visible = true;
  dialogEditVisible.title = "修改数据";
  Object.assign(formData, row);
}

//发布告示
async function addInfoClick() {
  await publishNotice(addData)
    .then(() => {
      ElMessage.success("发布告示成功");
      dialogaddVisible.visible = false;
      updateInfo();
    })
    .finally(() => (loading.value = false));
}

//编辑告示
async function editInfoClick(data: messageInfo) {
  loading.value = true;
  await updateNotice(data)
    .then(() => {
      ElMessage.success("修改成功！");
      dialogEditVisible.visible = false;
      updateInfo();
    })
    .finally(() => (loading.value = false));
}

//告示删除
async function deleteClick(id: number) {
  await ElMessageBox.confirm("确认删除该告示?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(function () {
    deleteNotice(id).then(() => {
      ElMessage.success("删除成功！");
      updateInfo();
    });
  });
}

//获取数据
async function getMessageInfo() {
  loading.value = true;
  await getMessage(queryParams)
    .then(({ data }) => {
      pageTotal.value = data.navigatepageNums[data.navigatepageNums.length - 1];
      dataList.value = data.messages;
      pageArr.value = data.navigatepageNums;
    })
    .catch((error) => {
      console.log("Error:", error);
    })
    .finally(() => {
      loading.value = false;
    });
}

function updateInfo() {
  getMessageInfo();
}

const handleUploadImage = (event: Event, insertImage: any, files: File[]) => {
  for (let i in files) {
    const formData = new FormData();
    formData.append("file", files[i]);
    axios.post(`http://localhost:8002/file/fileoss/upload`, formData).then(
      (response) => {
        const { data } = response.data;
        insertImage({
          url: data.url,
          desc: "desc",
        });
      },
      (error) => {
        ElMessage.error(error.message);
      }
    );
  }
};

const indexMethod = (index: number) => {
  return index + (queryParams.pageNum - 1) * queryParams.pageSize + 1;
};

function pageChange(pageNum: number) {
  queryParams.pageNum = pageNum;
  getMessageInfo();
}

function sizeChange(sizeNum: number) {
  queryParams.pageSize = sizeNum;
  getMessageInfo();
}

onMounted(() => {
  getMessageInfo();
});
</script>

<style scoped>
.pagination {
  display: flex;
  justify-content: center;

  .pagination__item {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    margin: 0 5px;
    cursor: pointer;
    border: 1px solid #ccc;
  }

  .pagination__item.active {
    color: white;
    background-color: #4593ff;
  }
}
</style>
